﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Contract.Customer.Response.PortfolioManagement;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.PortfolioManagement
{
    public class RetrieveAccountPlanningServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveAccountPlanningRequest, RetrieveAccountPlanningResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveAccountPlanningServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveAccountPlanningRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.UserRecord == null || string.IsNullOrEmpty(request.UserRecord.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.UserRecord;
            }
            TextSearch = Request.TextSearch;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveAccountPlanningResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<AccountPlanningRecord> records = new List<AccountPlanningRecord>();
            List<AccountPlanning> listCustomer = new List<AccountPlanning>();
            Dictionary<string, Branch> branchDict = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchCodeSme);
            listCustomer = Providers.Portfolios.RetrieveAccountPlanningByDao(out totalRecord, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AccountPlanningRecord
                {
                    RegionName = x.Region,
                    BranchName = x.BranchName,
                    DaoCif = x.DaoCif,
                    DaoName = x.DaoName,
                    Cif = x.Cif,
                    CustName = x.CustName,
                    Industry = x.Industry,
                    ActionPlanT62016 = x.ActionPlanT62016,
                    KhHoTro = x.KhHoTro,
                    PaymentMethod = x.PaymentMethod,
                    SpBanCheo = x.SpBanCheo,
                    Plan2016 = x.Plan2016,
                    SpDangSuDung = x.SpDangSuDung,
                    ThuPhi2016 = x.ThuPhi2016.HasValue ? x.ThuPhi2016.Value.ToString("#,###", cul.NumberFormat) : 0.ToString("#,###", cul.NumberFormat),
                    TinhHinhKhaiThac = x.TinhHinhKhaiThac,
                    TyLeSuDungHanMuc = x.TyLeSuDungHanMuc,
                    VuongMac = x.VuongMac,
                    WalletShare = x.WalletShare,
                    MainSector = x.MainSector,
                    Status = x.Status.ToString()
                }).ToList();
            }
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }
    }
}
